package site.assad.ditype;

import org.springframework.beans.factory.FactoryBean;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/11/30 11:06
 * Description: 演示 Spring 提供的工厂类实现,同时实现通过逗号分隔的形式一次性为User配置所有properties；
 */
public class UserFactoryBean implements FactoryBean{

    //实现通过逗号分隔的形式，一次性配置所有User的properties，简化配置；
    private String userInfo;
    public String getUserInfo() {
        return userInfo;
    }
    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    public Object getObject() throws Exception {
        User user = new User();
        String[] infos = userInfo.split(",");
        user.setName(infos[0]);
        user.setAge(Integer.parseInt(infos[1]));
        user.setCity(infos[2]);
        return user;
    }

    @Override
    public Class<?> getObjectType() {
        return User.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
